package com.gitee.huxing.course.conc.conc03.tool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc03.tool
 * @ClassName: CountDownLatchDemo2
 * @Author: huxingmale
 * @DateTime: 2021-08-26 0:04
 */
public class CountDownLatchDemo2 {

    private final static int threadCount = 200;

    public static void main(String[] args) throws Exception{
        ExecutorService service = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            service.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        test(threadNum);
                    } catch (Exception ex){
                        ex.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }
        countDownLatch.await();
        System.out.println("==>所有程序员完成任务，项目顺利上线！");
    }

    private static void test(int threadNum) throws Exception{
        Thread.sleep(100L);
        System.out.println(String.format("程序员[%d]完成任务。。。", threadNum));
        Thread.sleep(100L);
    }
}
